package com.test.shop.ware.service.impl;

import com.test.common.utils.R;
import com.test.shop.ware.feign.ProduceFeignService;
import com.test.shop.ware.vo.SkuHasStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.common.utils.PageUtils;
import com.test.common.utils.Query;

import com.test.shop.ware.dao.WareSkuDao;
import com.test.shop.ware.entity.WareSkuEntity;
import com.test.shop.ware.service.WareSkuService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {


    @Autowired
    ProduceFeignService produceFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            wrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //先判断是否存在这个库存记录
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        List<WareSkuEntity> list = this.baseMapper.selectList(wrapper.eq("sku_id", skuId).eq("ware_id", wareId));
        if(list == null || list.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(0);
            //TODO 远程查询Sku的名称 失败也无需要回滚，try-catch即可。
            try{
                R r = produceFeignService.info(skuId);
                Map<String,Object> map = (Map<String, Object>) r.get("skuInfo");

                if(r.getCode() == 0){
                    wareSkuEntity.setSkuName((String) map.get("skuName"));
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            this.baseMapper.insert(wareSkuEntity);
        }else {
            this.baseMapper.addStock(skuId,wareId,skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {

        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            //查询当前sku的库存
            //select sum(stock-stock_locked) from wms_ware_sku where sku_id = ?
            Long count = baseMapper.getSkuStock(skuId);
            skuHasStockVo.setSkuId(skuId);
            skuHasStockVo.setHasStock(count == null? false:count > 0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}